package com.oursnail.web.servlet;

import com.oursnail.entity.User;
import com.oursnail.service.UserService;
import com.oursnail.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created By XJL On 2017/11/4
 */
@WebServlet(name = "ActiveServlet")
public class ActiveServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String code = request.getParameter("code");
        UserService userService = new UserServiceImpl();

        try {
            User user = userService.getUserByCode(code);
            if (user!=null){
                user.setState(1);
                user.setCode(null);
                System.out.println("++++++++++++++"+user);
                userService.update(user);
                request.setAttribute("msg","您以成功激活，请返回登录页面!");

            }
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("msg","激活失败！");
        }
        request.getRequestDispatcher("/msg.jsp").forward(request,response);

    }
}
